package com.achang.achangmall.ware.service.impl;

import com.achang.achangmall.ware.entity.PurchaseDetailEntity;
import com.achang.achangmall.ware.service.PurchaseDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.achang.common.utils.PageUtils;
import com.achang.common.utils.Query;

import com.achang.achangmall.ware.dao.PurchaseDao;
import com.achang.achangmall.ware.entity.PurchaseEntity;
import com.achang.achangmall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("stauts",1)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void mergePurchase(HashMap<String, Object> map) {
        Long purchaseId = (Long) map.get("purchaseId");
        if (StringUtils.isEmpty(purchaseId)){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(0);
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();

            List<Long> items = (List<Long>) map.get("item");
            Long finalPurchaseId = purchaseId;
            List<PurchaseDetailEntity> collect = items.stream().map(s -> {
                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                purchaseDetailEntity.setId(s);
                purchaseDetailEntity.setPurchaseId(finalPurchaseId);
                purchaseDetailEntity.setStatus(1);
                return purchaseDetailEntity;
            }).collect(Collectors.toList());

            purchaseDetailService.updateBatchById(collect);
            PurchaseEntity purchaseEntity1 = new PurchaseEntity();
            purchaseEntity1.setId(purchaseId);
            purchaseEntity1.setUpdateTime(new Date());
            this.updateById(purchaseEntity1);
        }
    }

    @Override
    public void received(List<Long> ids) {
        List<PurchaseEntity> collect = ids.stream().map(item -> {
            PurchaseEntity entity = this.getById(item);
            return entity;
        }).filter(i -> {
            if (i.getStatus() == 0 || i.getStatus() == 1) {
                return true;
            } else {
                return false;
            }
        }).map(s->{
            s.setStatus(2);
            s.setUpdateTime(new Date());
            return s;
        }).collect(Collectors.toList());

        this.updateBatchById(collect);

        collect.forEach(item->{
            List<PurchaseDetailEntity> list = purchaseDetailService.listDetailByPurchaseId(item.getId());
            List<PurchaseDetailEntity> collect1 = list.stream().map(f -> {
                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                purchaseDetailEntity.setId(f.getId());
                purchaseDetailEntity.setStatus(2);
                return purchaseDetailEntity;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(collect1);
        });


    }




}